﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NotaManager
{
    public static class RuficatorForUTF
    {
        private static Dictionary<char, string> _conversion =
            new Dictionary<char, string>() { 
            {' ', Encoding.UTF8.GetString(new byte[]{0x20})},
            {'<', Encoding.UTF8.GetString(new byte[]{0xD0, 0x90})},
            {'=', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x91})},
            {'>', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x92})},
            {'A', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x93})},
            {'B', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x94})},
            {'C', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x95})},
            {'D', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x96})},
            {'E', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x97})},
            {'F', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x98})},
            {'G', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x99})},
            {'H', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x9A})},
            {'I', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x9B})},
            {'J', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x9C})},
            {'K', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x9D})},
            {'L', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x9E})},
            {'M', Encoding.UTF8.GetString(new byte[]{ 0xD0,0x9F})},
            {'N', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xA0})},
            {'O', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xA1})},
            {'P', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xA2})},
            {'Q', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xA3})},
            {'R', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xA4})},
            {'S', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xA5})},
            {'T', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xA6})},
            {'U', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xA7})},
            {'V', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xA8})},
            {'W', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xA9})},
            {'X', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xAD})},
            {'Y', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xAE})},
            {'Z', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xAF})},
            {'[', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xB0})},
            {'^', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xB1})},
            {']', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xB2})},
            {'|', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xB3})},
            {'}', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xB4})},
            {'a', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xB5})},
            {'b', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xB6})},
            {'c', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xB7})},
            {'d', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xB8})},
            {'e', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xB9})},
            {'f', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xBA})},
            {'g', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xBB})},
            {'h', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xBC})},
            {'i', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xBD})},
            {'j', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xBE})},
            {'k', Encoding.UTF8.GetString(new byte[]{ 0xD0,0xBF})},
            {'l', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x80})},
            {'m', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x81})},
            {'n', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x82})},
            {'o', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x83})},
            {'p', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x84})},
            {'q', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x85})},
            {'r', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x86})},
            {'s', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x87})},
            {'t', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x88})},
            {'u', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x89})},
            {'v', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x8A})},
            {'w', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x8B})},
            {'x', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x8C})},
            {'y', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x8D})},
            {'z', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x8E})},
            {'{', Encoding.UTF8.GetString(new byte[]{ 0xD1,0x8F})}
            };

        private static Dictionary<char, string> _liters = new Dictionary<char, string>() 
        { 
            { ' ', "\u0020" },
            { '<', "\u90D0" },
            { '=', "\u91D0" },
            { '>', "\u92D0" },
            { 'A', "\u93D0" },
            { 'B', "\u94D0" },
            { 'C', "\u95D0" },
            { 'D', "\u96D0" },
            { 'E', "\u97D0" },
            { 'F', "\u98D0" },
            { 'G', "\uD099" },
            { 'H', "\uD09A" },
            { 'I', "\uD09B" },
            { 'J', "\uD09C" },
            { 'K', "\uD09D" },
            { 'L', "\uD09E" },
            { 'M', "\uD09F" },
            { 'N', "\uD0A0" },
            { 'O', "\uD0A1" },
            { 'P', "\uD0A2" },
            { 'Q', "\uD0A3" },
            { 'R', "\uD0A4" },
            { 'S', "\uD0A5" },
            { 'T', "\uD0A6" },
            { 'U', "\uD0A7" },
            { 'V', "\uD0A8" },
            { 'W', "\uD0A9" },
            { 'X', "\uD0AD" },
            { 'Y', "\uD0AE" },
            { 'Z', "\uD0AF" },
            { '[', "\uD0B0" },
            { '^', "\uD0B1" },
            { ']', "\uD0B2" },
            { '|', "\uD0B3" },
            { '}', "\uD0B4" },

            { 'a', "\uD0B5" },
            { 'b', "\uD0B6" },
            { 'c', "\uD0B7" },
            { 'd', "\uD0B8" },
            { 'e', "\uD0B9" },
            { 'f', "\uD0BA" },
            { 'j', "\uD0BB" },
            { 'h', "\uD0BC" },
            { 'i', "\uD0BD" },
            { 'g', "\uD0BE" },
            { 'k', "\uD0BF" },

            { 'l', "\uD180" },
            { 'm', "\uD181" },
            { 'n', "\uD182" },
            { 'o', "\uD183" },
            { 'p', "\uD184" },
            { 'q', "\uD185" },
            { 'r', "\uD186" },
            { 's', "\uD187" },
            { 't', "\uD188" },
            { 'u', "\uD189" },
            { 'v', "\uD18A" },
            { 'w', "\uD18B" },
            { 'x', "\uD18C" },
            { 'y', "\uD18D" },
            { 'z', "\uD18E" },
            { '{', "\uD18F" }
        };

        public static string ConvertToRus(this byte[] sourceString)
        {
            var temp = Encoding.GetEncoding(932).GetString(sourceString).ChopNames();
            var res = new StringBuilder();

            foreach (var c in temp)
            {
                if (_conversion.ContainsKey(c))
                {
                    res.Append(_conversion[c]);
                }
                else
                {
                    res.Append(c);
                }
            }

            return res.ToString().Replace("  ", " ");
        }

        public static string ConvertToEng(this byte[] sourceString)
        {
            var temp = Encoding.GetEncoding(932).GetString(sourceString).ChopNames();
            
            return temp.Replace("  ", " ");
        }

        private static string ChopNames(this string text)
        {

            // char [] st = new char[] { (char)0x81, (char)0x75 };
            // char[] end = new char[] { (char)0x81, (char)0x76 };
            string newstring = ""; //start = st.ToString(), end_str = end.ToString();
            bool flag = false;
            int start_pos = 0, end_pos = 0, num = 0;
            //newstring = text.Replace(start, " !!! ");
            // return text.Substring(
            foreach (char c in text)
            {
                if (c == 0x81)
                {
                    flag = true;

                }
                else if (flag)
                {
                    if (c == 0x75)
                    {
                        start_pos = num;
                    }
                    if (c == 0x76)
                    {
                        end_pos = num;
                    }
                    flag = false;

                }
                num++;
            }
            if (start_pos != 0)
                newstring = text.Substring(start_pos + 1, end_pos - start_pos - 1);
            else newstring = text;

            return newstring;

        }

    }
}
